<template>
    <div class="EditPostionGradesPopup">
        <popup ref="popupRef" :title="popupTitle" :async="true" width="550px" @confirm="handleSubmit" @close="handleClose">
            <el-form ref="formRef" :model="formData" label-width="84px" :rules="formRules">
				<el-form-item label="层级名称" prop="Name">
				    <el-input v-model="formData.Name" placeholder="请输入层级名称" clearable />
				</el-form-item>
				<el-form-item label="层级起算日期" prop="GradeStartDate">
                    <el-date-picker v-model="formData.GradeStartDate" type="date" />
                </el-form-item>
				<el-form-item label="层级终止日期" prop="GradeEndDate">
                    <el-date-picker v-model="formData.GradeEndDate" type="date" />
                </el-form-item>
				<el-form-item label="职级起算日期" prop="LevelStartDate">
                    <el-date-picker v-model="formData.LevelStartDate" type="date" />
                </el-form-item>
				<el-form-item label="批准日期" prop="PassDate">
                    <el-date-picker v-model="formData.PassDate" type="date" />
                </el-form-item>
				<el-form-item label="批准机关名称" prop="AppointmentOrganizationName">
                    <el-input v-model="formData.AppointmentOrganizationName" placeholder="请输入批准机关名称" clearable />
                </el-form-item>
				<el-form-item label="批准机关代码" prop="AppointmentOrganizationCode">
                    <el-input v-model="formData.AppointmentOrganizationCode" placeholder="请输入批准机关代码" clearable />
                </el-form-item>
				<el-form-item label="状态" prop="LogStatus">
                    <el-input v-model="formData.LogStatus" placeholder="请输入状态" clearable />
                </el-form-item>
				<el-form-item label="任职文号" prop="OrderNumber">
                    <el-input v-model="formData.OrderNumber" clearable />
                </el-form-item>
				<el-form-item label="变动原因" prop="Remark">
                    <el-input v-model="formData.Remark" clearable />
                </el-form-item>
            </el-form>
        </popup>
    </div>
</template>
<script lang="ts" setup name="EditPostionGradesPopup">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'

import { EditPostionGrades } from '@/api/consumer'

const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑学历信息' : '添加学历信息'
})
const props = defineProps(['member_id']) /**从view.vue传递参数 */
const formData = reactive({
    id: 0,
    member_id: props.member_id,
     Name: '',
     GradeStartDate: '',
     GradeEndDate: '',
     LevelStartDate: '',
     PassDate: '',
     AppointmentOrganizationName: '',
	 LogStatus: '',OrderNumber: '',Remark: '',
     AppointmentOrganizationCode: ''
})

const formRules = reactive({
     Name: [
         {
             required: true,
             message: '请输入层级名称',
             trigger: ['blur']
         }
     ],
     GradeStartDate: [
         {
             required: true,
             message: '请输入层级起算日期',
             trigger: ['blur']
         }
     ],
     AppointmentOrganizationName: [
         {
             required: true,
             message: '请输入批准机关名称',
             trigger: ['blur']
         }
		],
    
})


const handleSubmit = async () => {
    await formRef.value?.validate()
    console.log('职务层级表单内容', formData)
    await EditPostionGrades(formData)
    //mode.value == 'edit' ? await adminEdit(formData) : await adminAdd(formData)
    popupRef.value?.close()
    emit('success')
}

const open = (type = 'add') => {
    mode.value = type
    popupRef.value?.open()
}

const setFormData = async (row: any) => {
    formData.id = row.id
    const data = await EditPostionGrades({
        id: row.id,
    		 mode:'get'
    })
    for (const key in formData) {
        if (data[key] != null && data[key] != undefined) {
            formData[key] = data[key]
        }
    }
}

const handleClose = () => {
    emit('close')
}

defineExpose({
    open,
    setFormData
})
</script>
